#pragma once
#include <QString>
#include <QIcon>
#include <QUuid>
#include <QDateTime>
#include <QFile>
#include <QFileInfo>

// 封装一个 "宏" 作为打印日志的方式.
#define TAG QString("[%1:%2]").arg(model::getFileName(__FILE__), QString::number(__LINE__))
// #define TAG "[" << __LINE__ << "]"

// qDebug 打印字符串的时候, 就会自动加上 " "
#define LOG() qDebug().noquote() << TAG

namespace model {

static inline QString getFileName(const QString& path) {
    QFileInfo fileInfo(path);
    return fileInfo.fileName();
}

static inline QString formatTime(int64_t timeStamp)
{
    QDateTime datetime = QDateTime::fromSecsSinceEpoch(timeStamp);
    return datetime.toString("MM-dd HH:mm:ss");
}

// 获取时间戳
static inline int64_t getTime() {

    return QDateTime::currentSecsSinceEpoch();
}


// 从指定文件中读取内容构成一个QByteArray
static inline QByteArray loadFileToByteArray(const QString& path){
    QFile file(path);
    if(!file.open(QFile::ReadOnly))
    {
        qDebug() << "文件打开失败";
        return QByteArray();
    }
    QByteArray content = file.readAll();
    file.close();
    return content;
}


// 将QByteArray中的内容写出到文件中
static inline void writeByteToFile(const QString& path, const QByteArray& content){
    QFile file(path);
    if(!file.open(QFile::WriteOnly)){
        qDebug() << "文件打开失败";
        return;
    }
    file.write(content);
    file.flush();
    file.close();
}

////////////////////////
/// 用户信息
////////////////////////
class UserInfo{
public:
    QString userId = "";      // 用户id
    QString nickname = "";    // 用户昵称
    QString description = ""; // 用户签名
    QString phone = "";       // 手机号码
    QIcon avatar;             // 用户头像
};

////////////////////////
/// 消息信息
////////////////////////
enum MessageType{
    TEXT_TYPE,  // 文本消息
    IMAGE_TYPE, // 图片消息
    FILE_TYPE,  // 文件消息
    VOICE_TYPE  // 语音消息
};


class Message{
public:
    QString messageId = "";                 // 消息的id
    QString chatSessionId = "";             // 消息所属会话的会话id
    QString time = "";                      // 消息发出的时间
    MessageType messageType = TEXT_TYPE;    // 消息类型
    UserInfo sender;                        // 消息发送者
    QByteArray content = "";                // 正文信息
    QString fileid = "";                    // 发送文件类型时文件的id，对方可以拿着文件id下载文件
    QString fileName = "";                  // 文件名，可以先让接受方接受到文件名而不是文件内容，提高通信速率

    // 由于不同类型的消息会构造不同的Message对象，因此我们使用工厂模式编写构造函数
    static Message makeMessage(QString chatSessionId, MessageType messageType
                             , UserInfo sender, QByteArray content, QString fileName)
    {
        if(TEXT_TYPE == messageType){
            makeTextMessage(chatSessionId, sender, content);
        }else if(IMAGE_TYPE == messageType){
            makeImageMessage(chatSessionId, sender, content);
        }else if(FILE_TYPE == messageType){
            makeFileMessage(chatSessionId, sender, content, fileName);
        }else if(VOICE_TYPE == messageType){
            makeVoiceMessage(chatSessionId, sender, content);
        }else{
            return Message();
        }
    }

private:
    static inline QString makeid()
    {
        return "M" + QUuid::createUuid().toString().sliced(25, 12);
    }

    static Message makeTextMessage(QString chatSessionId, UserInfo sender, QByteArray content)
    {
        Message message;
        message.messageId = makeid();
        message.chatSessionId = chatSessionId;
        message.time = formatTime(getTime());
        message.content = content;
        message.sender = sender;

        message.messageType = TEXT_TYPE;
        message.fileid = "";
        message.fileName = "";

        return message;
    }

    static Message makeImageMessage(QString chatSessionId, UserInfo sender, QByteArray content)
    {
        Message message;
        message.messageId = makeid();
        message.chatSessionId = chatSessionId;
        message.time = formatTime(getTime());
        message.content = content;
        message.sender = sender;

        message.messageType = IMAGE_TYPE;
        message.fileid = "";
        message.fileName = "";

        return message;
    }

    static Message makeFileMessage(QString chatSessionId, UserInfo sender, QByteArray content, QString fileName)
    {
        Message message;
        message.messageId = makeid();
        message.chatSessionId = chatSessionId;
        message.time = formatTime(getTime());
        message.content = content;
        message.sender = sender;

        message.messageType = FILE_TYPE;
        message.fileid = "";
        message.fileName = fileName;

        return message;
    }

    static Message makeVoiceMessage(QString chatSessionId, UserInfo sender, QByteArray content)
    {
        Message message;
        message.messageId = makeid();
        message.chatSessionId = chatSessionId;
        message.time = formatTime(getTime());
        message.content = content;
        message.sender = sender;

        message.messageType = VOICE_TYPE;
        message.fileid = "";
        message.fileName = "";

        return message;
    }
};



////////////////////////
/// 会话信息
////////////////////////
class ChatSessionInfo{
public:
    QString chatSessionId = "";   // 会话id
    QString chatSeesionName = ""; // 会话名称，如果是单聊，就是对方呢称；如果是群聊，就是群聊名称
    QString lastMessage = "";     // 会话中最新的一条消息
    QIcon avatar;                 // 会话头像。如果是单聊，就是对方的头像；如果是群聊，就是群聊的头像

};




}
